package com.welink.cmp.topotree.command;

import com.welink.cmp.convertor.GenericConvertor;
import com.welink.cmp.dao.tree.ServiceTreeDaoService;
import com.welink.cmp.dao.tree.dataobejct.ServiceTreeDO;
import com.welink.cmp.domain.topotree.IServiceTreeGateway;
import com.welink.cmp.dto.topotree.BuildServiceTopologyAddCmd;
import com.welink.cmp.dto.topotree.ModifyServiceTopologyUptCmd;
import com.welink.cmp.dto.topotree.ServiceTreeCO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author yanwei li create  2024-06-04 18:38
 */
@Component
public class ServiceTopoExe {
    @Autowired
    private IServiceTreeGateway serviceTreeGateway;

    @Autowired
    private ServiceTreeDaoService serviceTreeDaoService;

    public Boolean save(BuildServiceTopologyAddCmd addCmd){
        ServiceTreeDO serviceTreeDO = new ServiceTreeDO();
        BeanUtils.copyProperties(addCmd,serviceTreeDO);
        if (addCmd.isNeedsOperator()){
            serviceTreeDO.setCreateBy(addCmd.getOperator());
        }
        final boolean save = serviceTreeDaoService.save(serviceTreeDO);
        // TODO 未来添加缓存 维护
        return save;
    }

    public Boolean update(ModifyServiceTopologyUptCmd uptCmd){
        ServiceTreeDO serviceTreeDO = new ServiceTreeDO();
        BeanUtils.copyProperties(uptCmd,serviceTreeDO);
        if (uptCmd.isNeedsOperator()){
            serviceTreeDO.setUpdateBy(uptCmd.getOperator());
        }
        final boolean b = serviceTreeDaoService.updateById(serviceTreeDO);
        // TODO  清楚缓存 或者维护缓存
        return b;
    }

    public ServiceTreeCO getServiceTreeById(Long id){
        final ServiceTreeDO byId = serviceTreeDaoService.getById(id);
        return GenericConvertor.toCO(byId, ServiceTreeCO.class);
    }

    public String getServiceTreeByHostId(Long hostId){
        final String svcTree = serviceTreeGateway.getServiceTreeByHostId(hostId);
        return svcTree;
    }

    public List<Long> getHostIdsByServiceTree(String svcTree){
        final List<Long> hostIdsByTree = serviceTreeGateway.getHostIdsByTree(svcTree);
        return hostIdsByTree;
    }

    public Boolean saveSvcTreeBatch(Map<Long,String> map){
        if (map.isEmpty()){
            return Boolean.FALSE;
        }
        return serviceTreeGateway.saveServiceTreeBatchByMap(map);
    }
}
